package com.wuyou.simple;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 2363. 合并相似的物品
 */
public class MergeSimilarItems {


    public List<List<Integer>> sol_1(int[][] items1, int[][] items2){
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < items1.length; i ++){
            Integer tempDef = map.getOrDefault(items1[i][0], 0);
            map.put(items1[i][0], items1[i][1] + tempDef);
        }

        for (int i = 0; i < items2.length; i ++){
            Integer tempDef = map.getOrDefault(items2[i][0], 0);
            map.put(items2[i][0], items2[i][1] + tempDef);
        }

        List<List<Integer>> ret = new ArrayList<>(map.keySet().size());
        for(Integer key : map.keySet()){
            List<Integer> tempList = new ArrayList<>();
            tempList.add(key);
            tempList.add(map.get(key));
            ret.add(tempList);
        }
        ret.sort(new Comparator<List<Integer>>() {
            @Override
            public int compare(List<Integer> o1, List<Integer> o2) {
                return o1.get(0) - o2.get(0);
            }
        });
        return ret;
    }
}
